package com.yoomiito.app.pay;

import android.app.Activity;
import android.text.TextUtils;

import com.alipay.sdk.app.H5PayCallback;
import com.alipay.sdk.app.PayTask;
import com.alipay.sdk.util.H5PayResultModel;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.yoomiito.app.Constant;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.bean.PayOrderInfo;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 微信、支付宝支付
 */
public class PayTypeUtils {

    public static void wxPay(PayOrderInfo orderResult) {
        PayReq pr = new PayReq();
        pr.appId = orderResult.getAppid();
        pr.partnerId = orderResult.getPartnerid();
        pr.prepayId = orderResult.getPrepayid();
        pr.packageValue = orderResult.getPackageX();
        pr.nonceStr = orderResult.getNoncestr();
        pr.timeStamp = orderResult.getTimestamp();
        pr.sign = orderResult.getSign();
        App.msgApi.sendReq(pr);
    }

    public static void aliPay(BaseActivity context, String orderInfo) {
        Observable.just(1)
                .subscribeOn(Schedulers.io())
                .map(integer -> {
                    PayTask alipay = new PayTask(context);
                    return alipay.payV2(orderInfo, true);
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(stringStringMap -> {
                    AliPayResult payResult = new AliPayResult(stringStringMap);
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    EventMessage message = new EventMessage();
                    message.setEventType(Constant.PAY_RESULT);
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        message.setCode(AllCode.Success);
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                    } else {
                        message.setCode(AllCode.Fail);
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
//                        Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
                    }
                    EventBus.getDefault().post(message);
//                    if (onPayResultListener!=null)onPayResultListener.onResult(message);
                });
    }

    /**
     * H5支付转app支付
     */
    public static boolean aliH5ToAppPay(Activity activity,String url,AliPayUrlCallBack callBack){
        PayTask payTask = new PayTask(activity);
        return payTask.payInterceptorWithUrl(url,true,h5PayResultModel -> {
            String resultUrl = h5PayResultModel.getReturnUrl();
            L.i("返回的Url:"+resultUrl);
            if (!TextUtils.isEmpty(resultUrl)){
                Observable.just(1)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(returnUrl -> {
                            callBack.url(resultUrl);
                        });
            }
        });
    }

}
